import os

# 设置代理
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"

# 设置本地缓存目录
cache_dir = os.path.join('D:', os.path.sep, 'ModelSpace', 'Cache')
os.environ['HF_HOME'] = cache_dir

from transformers import pipeline

# 创建Pipeline任务
nlp = pipeline("table-question-answering", model="google/tapas-base-finetuned-wtq")

# 执行表格问答任务
if __name__ == "__main__":
    # 表格数据
    table = [
        {"Year": "2020", "Quarter": "Q1", "Revenue": "500M", "Profit": "100M"},
        {"Year": "2020", "Quarter": "Q2", "Revenue": "550M", "Profit": "110M"},
        {"Year": "2020", "Quarter": "Q3", "Revenue": "600M", "Profit": "120M"},
        {"Year": "2020", "Quarter": "Q4", "Revenue": "650M", "Profit": "130M"},
        # 更多行...
    ]

    # 执行任务
    result = nlp(table=table, query="What was the profit in Q4 of 2020?")

    print(result)
    # 输出：{'answer': '130M', 'coordinates': [(3, 3)], 'cells': ['130M'], 'aggregator': 'NONE'}
